package com.uenpay.im.ui.old.activity.wallet;

import android.app.Dialog;
import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.uenpay.im.R;

import java.util.ArrayList;


public class BottomDialogWalletSubject {

	public interface IItemOnClick {
		public abstract void onItemClick(String type);
	}

	public static void showBottomDialog(Context mContext,
			final String selectedType,final ArrayList<String> arrayList, final IItemOnClick mIItemOnClick) {
		final Dialog dialog = new Dialog(mContext, R.style.pop_dialog);
		dialog.setContentView(R.layout.pop_dialog_wallet_subject);
		Window win = dialog.getWindow();
		win.setGravity(Gravity.BOTTOM);
		win.setWindowAnimations(R.style.dialogWindowAnim);
		dialog.setCanceledOnTouchOutside(true);
		dialog.show();
		win.setLayout(ViewGroup.LayoutParams.MATCH_PARENT,
				ViewGroup.LayoutParams.WRAP_CONTENT);
		ListView listview = (ListView) dialog.findViewById(R.id.listview);
		StatementSubjectAdapter adapter = new StatementSubjectAdapter(mContext,
				selectedType,arrayList);
		listview.setAdapter(adapter);
		listview.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				String type = (String) arg0.getAdapter().getItem(arg2);
				mIItemOnClick.onItemClick(type);
				dialog.dismiss();
			}
		});
	}

	private static class StatementSubjectAdapter extends BaseAdapter {
		private String selectedType;
		private ArrayList<String> types = null;
		private Context mContext;

		public StatementSubjectAdapter(Context _Context, String _SelectedType,ArrayList<String> arrayList) {
			selectedType = _SelectedType;
			mContext = _Context;
			types =arrayList;
		}

		@Override
		public int getCount() {
			return types.size();
		}

		@Override
		public Object getItem(int arg0) {
			return types.get(arg0);
		}

		@Override
		public long getItemId(int arg0) {
			return arg0;
		}

		@Override
		public View getView(int arg0, View arg1, ViewGroup arg2) {
			SubjectViewHodler mSubjectViewHodler = null;
			if (arg1 == null) {
				mSubjectViewHodler = new SubjectViewHodler();
				arg1 = View.inflate(mContext,
						R.layout.item_statement_subject_layout, null);
				mSubjectViewHodler.is_selected = (ImageView) arg1
						.findViewById(R.id.is_selected);
				mSubjectViewHodler.subjectName = (TextView) arg1
						.findViewById(R.id.subject_name_tv);
				arg1.setTag(mSubjectViewHodler);
			} else {
				mSubjectViewHodler = (SubjectViewHodler) arg1.getTag();
			}

			if (selectedType.equals(types.get(arg0))) {
				mSubjectViewHodler.is_selected.setVisibility(View.VISIBLE);
			} else {
				mSubjectViewHodler.is_selected.setVisibility(View.GONE);
			}
			mSubjectViewHodler.subjectName.setText(types.get(arg0));
			 
			return arg1;
		}

		class SubjectViewHodler {
			private TextView subjectName;
			private ImageView is_selected;
		}

	}

}
